﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using Infragistics.Controls.Editors;
using Infragistics.Controls.Menus;
using VNet.VisiSuite.Common;
using VNet.VisiSuite.Controls;
using VNet.VisiSuite.VisiBoard.Controls;
using VNet.VisiSuite.VisiBoard.Models;
using VNet.VisiSuite.VisiBoard.ViewModels;
using VNet.VisiSuite.VisiBoard.Dal.EntityClasses;
using VNet.VisiSuite.VisiBoard.Common.Helpers;

namespace VNet.VisiSuite.VisiBoard.Views
{
    public partial class ScrapDialogView : VisiDialog
    {
        private ScrapDialogViewModel _model;
        private DateTime _start_time;

        public ScrapDialogView()
        {
            InitializeComponent();
        }

        public ScrapDialogView(DateTime startTime)
        {
            InitializeComponent();
            _start_time = startTime;
            var menu_items = new List<VNetMenuItem>();
            menu_items.Add(new VNetMenuItem(null, "OK", CommonConstants.VNetIcon.AppbarCheck, VNetMenuItemType.WhiteBackgroundTheme, onOK));
            menu_items.Add(new VNetMenuItem(null, "Cancel", CommonConstants.VNetIcon.AppbarClose, VNetMenuItemType.WhiteBackgroundTheme, onCancel));
            _menu.Icons = menu_items;
            _menu.Icons[0].IsEnabled = false;
            _bottom_bar.Icons = new List<VNetMenuItem>
            {
                new VNetMenuItem(null, "Input Keyboard",CommonConstants.VNetIcon.AppbarInputKeyboard,VNetMenuItemType.WhiteBackgroundTheme, onKeyboard)
            };
            _model = DataContext as ScrapDialogViewModel;
        }

        private void VisiDialog_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void onOK()
        {
            Cancelled = false;
            close();
        }
        private void onKeyboard()
        {
            RaiseInputKeyboardClick();
        }

        private void onCancel()
        {
            Cancelled = true;
            close();
        }

        public DateTime When
        {
            get { return _start_time; }
        }

        public int Quantity
        {
            get {
                int qty = 0;
                int.TryParse(_numeric_scrap_quantity.Value.ToString(), out qty);
                return qty;
            }
        }

        public dynamic ProductionPlanData
        {
            get {
                dynamic ppe = null;
                if (_combo_production_plan.SelectedItem != null)
                    ppe = (dynamic)_combo_production_plan.SelectedItem;
                return ppe;
            }
        }

        public ReasonCodeEntity ScrapReasonCode
        {
            get
            {
                ReasonCodeEntity selected = null;
                if (_scrap_reasons.SelectedDataItems != null && _scrap_reasons.SelectedDataItems.Any())
                    selected = (ReasonCodeEntity)((SimpleNode)_scrap_reasons.SelectedDataItems.First()).Object;
                return selected;
            }
        }

        private void production_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            updateOkButtonEnabled();
        }

        private void updateOkButtonEnabled()
        {
            _menu.Icons[0].IsEnabled = ProductionPlanData != null
                    &&  ScrapReasonCode != null
                    && Quantity > 0;
        }


        private void _scrap_reasons_OnSelectedNodesCollectionChanged(object sender, NodeSelectionEventArgs e)
        {
            updateOkButtonEnabled();
        }

        private void _numeric_scrap_quantity_OnLostFocus(object sender, RoutedEventArgs e)
        {
            updateOkButtonEnabled();
        }
    }
}
